package com.boomsecret.threadtransfer;

/**
 * @author wangjinliang on 2019/1/31.
 */
public class WaitNotifyDemo {

    public static void main(String[] args) {
        Object object = new Object();
        ThreadA threadA = new ThreadA(object);
        ThreadB threadB = new ThreadB(object);

        threadA.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadB.start();
    }

    public static class ThreadA extends Thread {
        private Object object;

        public ThreadA(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println("threadA start.");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("threadA end.");
            }
        }
    }

    public static class ThreadB extends Thread {
        private Object object;

        public ThreadB(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println("threadB start.");
                object.notify();
                System.out.println("threadB end.");
            }
        }
    }
}
